layui.use(['form','layer','table','laytpl','tree'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;

    $.ajax({
        type: "post",
        url: "/dept/tree",
        datatype: "json",
        contenttype: "application/json; charset=utf-8",
        success: function (treeData) {
            // 部门树
            layui.tree({
                elem: '#departmentTree'
                , nodes: treeData
                , click: function (node) {
                    // 叶子节点执行查询
                    if(node.children == null){
                        table.reload("userListTable",{
                            url: "/user/getUserByDeptId",
                            page: {
                                curr: 1 //重新从第 1 页开始
                            },
                            where: {
                                deptId: node.id
                            }
                        })
                    }
                }
            });
        }
    })

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        url : '/user/getUserByDeptId',
        cellMinWidth : 65,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        id : "userListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: 'username', title: '用户名', minWidth:100, align:"center"},
            {field: 'nickname', title: '用户昵称', minWidth:100, align:"center"},
            {field: 'email', title: '用户邮箱', minWidth:200, align:'center',templet:function(d){
                return '<a class="layui-blue" href="mailto:'+d.email+'">'+d.email+'</a>';
            }},
            {field: 'sex', title: '用户性别', align:'center',templet:function(d){
                return d.sex == 0 ? '女':'男';
            }},
            {field: 'mobile', title: '手机号', align:'center'},
            {title: '操作', minWidth:220, templet:'#userListBar', fixed:"right", align:"center"}
        ]]
    });

    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            edit(data);
        }else if(layEvent === 'allot'){ //分配权限
            allot(data);
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此角色？',{icon:3, title:'提示信息'},function(index){
                $.get("#",{
                    userIds : data.id  //将需要删除的roleId作为参数传入
                },function(data){
                    tableIns.reload();
                    layer.close(index);
                    layer.msg("删除成功");
                })
            });
        }
    });

    //编辑用户
    function allot(edit) {
        var index = layui.layer.open({
            title: "分配角色",
            type: 2,
            content: "/page/common/allotRole",
            area: ['500px', '500px'],
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                if (edit) {
                    body.find(".userId").val(edit.id);  //id
                    body.find(".username").val(edit.username);  //username
                    form.render();
                }
                setTimeout(function () {
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                }, 500)
            }
        })
    }

})